﻿using DevComponents.DotNetBar;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VMind.Global;

namespace PPIDesktopClient.Controls.QueryAssetTree
{
    public partial class QueryAssetTree : UserControl
    {
        #region Private Members
        private bool m_ExpandAll = false;
        private List<EUtility_Query_Type> m_QueryTypes = null;
        private List<EUtility_QueryAsset> m_QueryAssets = null;
        private TreeNodeEx m_OldSelectedNode = null;
        #endregion

        #region Private Methods
        private void AddNodes()
        {
            try
            {
                TreeNode[] _TypeNodes = new TreeNode[m_QueryTypes.Count];
                for (Int32 i = 0; i < m_QueryTypes.Count; i++)
                    _TypeNodes[i] = new TreeNodeEx("t" + m_QueryTypes[i].Utility_Query_TypeId.ToString(), m_QueryTypes[i].Name + " Queries", new TreeNodeEx[0]);
                FillNodes(_TypeNodes);
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void FillNodes(TreeNode[] typeNodes)
        {
            foreach (TreeNode _TypeNode in typeNodes)
                tvwMain.Nodes.Add(_TypeNode);
            bool _Found;
            foreach (EUtility_QueryAsset _QueryAsset in m_QueryAssets)
            {
                _Found = false;
                for (Int32 i = 0; i < tvwMain.Nodes.Count && !_Found; i++)
                    if ("t" + _QueryAsset.Utility_Query_Type.Utility_Query_TypeId.ToString() == ((TreeNodeEx)tvwMain.Nodes[i]).Value)
                    {
                        tvwMain.Nodes[i].Nodes.Add(new TreeNodeEx("q" + _QueryAsset.Utility_QueryAssetId.ToString(), _QueryAsset.Name, new TreeNodeEx[0]));
                        _Found = true;
                    }
            }
        }
        #endregion

        #region Public Properties
        public bool ExpandAll
        {
            get { return m_ExpandAll; }
            set { m_ExpandAll = value; }
        }
        public List<EUtility_QueryAsset> QueryAssets
        {
            get { return m_QueryAssets; }
            set { m_QueryAssets = value; }
        }
        public List<EUtility_Query_Type> QueryTypes
        {
            get { return m_QueryTypes; }
            set { m_QueryTypes = value; }
        }
        public TreeNodeEx SelectedItem
        {
            get { return (TreeNodeEx)tvwMain.SelectedNode; }
            set { tvwMain.SelectedNode = value; }
        }
        #endregion

        #region Public Methods
        public override void Refresh()
        {
            //Suppress repainting the TreeView until all the objects have been created.
            tvwMain.BeginUpdate();
            //Clear the TreeView each time the method is called.
            tvwMain.Nodes.Clear();

            AddNodes();

            if (m_ExpandAll)
                tvwMain.ExpandAll();

            //Begin repainting the TreeView.
            tvwMain.EndUpdate();
        }
        public void SelectQueryAssets(List<EUtility_QueryAsset> selectedQueryAssets)
        {
            List<Int32> _SelectedIds = new List<Int32>();
            foreach (EUtility_QueryAsset _QueryAsset in selectedQueryAssets)
                _SelectedIds.Add(_QueryAsset.Utility_QueryAssetId);
            SelectQueryAssets(_SelectedIds);
        }
        public void SelectQueryAssets(List<Int32> selectedIds)
        {
            tvwMain.BeginUpdate();
            foreach (TreeNodeEx _Node in tvwMain.Nodes)
            {
                if (!_Node.Checked && _Node.Value.Substring(0, 1) == "q" && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value.Substring(1))))
                {
                    _Node.Checked = true;
                    m_QueryAssets.Add(new EUtility_QueryAsset(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                if (_Node.Nodes.Count > 0)
                    CheckQueryChildren(selectedIds, _Node);
            }
            tvwMain.EndUpdate();
        }
        void CheckQueryChildren(List<Int32> selectedIds, TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                if (!_Node.Checked && _Node.Value.Substring(0, 1) == "q" && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value.Substring(1))))
                {
                    _Node.Checked = true;
                    m_QueryAssets.Add(new EUtility_QueryAsset(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                if (_Node.Nodes.Count > 0)
                    CheckQueryChildren(selectedIds, _Node);
            }
        }
        #endregion

        #region Constructor
        public QueryAssetTree()
        {
            InitializeComponent();
            m_QueryTypes = new List<EUtility_Query_Type>();
            m_QueryAssets = new List<EUtility_QueryAsset>();
        }
        #endregion

        #region Events
        public event TreeViewEventHandler CheckedChange;
        public event EventHandler ClickedNew;
        public event EventHandler ClickedEdit;
        public event EventHandler ClickedDelete;
        public event EventHandler SelectedNode;
        public event EventHandler DeselectedNode;


        private void OnCheckedChanged(object child, TreeViewEventArgs e)
        {
            // Safely invoke an event:
            if (CheckedChange != null)
                CheckedChange(child, e);
        }
        private void OnClickedNew(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedNew != null)
            {
                ClickedNew(o, e);
            }
        }
        private void OnClickedEdit(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedDelete(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }
        private void OnSelectedNode(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (SelectedNode != null)
            {
                SelectedNode(o, e);
            }
        }
        private void OnDeselectedNode(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (DeselectedNode != null)
            {
                DeselectedNode(o, e);
            }
        }

        void tvwMain_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            TreeNodeEx _Node = (TreeNodeEx)e.Node;
            switch (_Node.Value.Substring(0, 1))
            {
                case "t":
                    foreach (EUtility_Query_Type _Type in m_QueryTypes)
                        if (_Type.Utility_Query_TypeId == ConvertHelper.ToInt32(_Node.Value.Substring(1)))
                            OnCheckedChanged(_Type, e);
                    break;
                case "q":
                    foreach (EUtility_QueryAsset _QueryAsset in m_QueryAssets)
                        if (_QueryAsset.Utility_QueryAssetId == ConvertHelper.ToInt32(_Node.Value.Substring(1)))
                            OnCheckedChanged(_QueryAsset, e);
                    break;
            }
            if (_Node.Level == 0)
            {
                foreach (TreeNode n in _Node.Nodes)
                {
                    n.Checked = _Node.Checked;
                }
            }
        }

        private void cmiNewAsset_Click(object sender, EventArgs e)
        {
            OnClickedNew(sender, e);
        }

        private void cmiEditAsset_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, e);
        }

        private void cmiDeleteAsset_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, e);
        }

        private void tvwMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNodeEx node = (TreeNodeEx)e.Node;
            if (node != null && node.Level != 0)
            {
                if (!node.Equals(m_OldSelectedNode))
                {
                    tvwMain.SelectedNode = node;
                    OnSelectedNode(sender, e);
                }
            }
            else
            {
                OnDeselectedNode(sender, e);
            }
        }
        private void tvwMain_Leave(object sender, EventArgs e)
        {
            OnDeselectedNode(sender, e);
        }
        #endregion

       



    }
}
